﻿using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using UnityEngine;

namespace HappyStudent.CfgExport
{
    public static class CfgExport
    {   /// <summary>
        /// 导出配置
        /// </summary>
        /// <param name="OutCfgPath"></param>
        public static void OutCfg(string OutCfgPath, ConcurrentDictionary<string, PropertyInfo> properties)
        {
            var info = Directory.CreateDirectory(OutCfgPath);
            if (!Directory.Exists(OutCfgPath))
            {
                Debug.LogError("Configuration directory creation failed, please check the configuration output path");
                return;
            }
            try
            {
                Parallel.ForEach(properties, property =>
                {
                    string name = property.Key;
                    var values = property.Value.GetValue(null);
                    if (values != null && values is IDictionary dic)
                    {
                        string path = $"{OutCfgPath}\\{name}.json";
                        using (JsonTextWriter jtw = new JsonTextWriter(new StreamWriter(path))
                        {
                            Indentation = 2,
                            Formatting = (Formatting)1
                        })
                        {
                            IDictionaryEnumerator enumerator = dic.GetEnumerator();
                            jtw.WriteStartObject();
                            while (enumerator.MoveNext())
                            {
                                jtw.WritePropertyName(enumerator.Key.ToString());
                                jtw.WriteStartObject();
                                foreach (var prp in enumerator.Value.GetType().GetFields())
                                {

                                    var temp = prp.GetValue(enumerator.Value);
                                    if (temp != null)
                                    {
                                        jtw.WritePropertyName(prp.Name);
                                        if (temp is IList em)
                                        {
                                            WriteList(jtw, em);
                                        }
                                        else
                                        {
                                            jtw.WriteValue(temp);
                                        }
                                    }
                                }
                                jtw.WriteEndObject();
                            }
                            jtw.WriteEndObject();
                        }
                    }
                    else
                    {
                       // Debug.LogWarning($"{name} not find");
                    }
                });
            }
            catch (Exception e)
            {
                Debug.LogError(e.ToString());
            }
        }

        private static void WriteList(JsonTextWriter jtw, object input)
        {
            if (input is IList list)
            {
                jtw.WriteStartArray();
                foreach (var innerList in list)
                {
                    if (innerList is IList Ks)
                    {
                        WriteList(jtw, Ks);
                    }
                    else
                    {
                        jtw.WriteValue(innerList);
                    }
                }
                jtw.WriteEndArray();
            }
        }
    }
}
